package cn.net.isir.service2;

import com.github.pagehelper.PageInfo;
import cn.net.isir.core.BasicService2;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.dao.CategoryDao;
import cn.net.isir.dao.GoodsDao;
import cn.net.isir.dao.GoodsSpecificationDao;
import cn.net.isir.dao.PicsDao;
import cn.net.isir.entity.Category;
import cn.net.isir.entity.Goods;
import cn.net.isir.entity.GoodsSpecification;
import cn.net.isir.entity.Pics;
import cn.net.isir.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GoodsService extends BasicService2<Goods> {
    @SetBasicDao
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private GoodsSpecificationDao goodsSpecificationDao;
    @Resource
    private PicsDao picsDao;

    /**
     * 新增菜品
     *
     * @param goods 菜品
     * @return 成功是1
     */
    @Override
    public int insert(Goods goods) {
        goodsDao.insert(goods);
        if (StringUtils.isBlank(goods.getId())) {
            return 0;
        }
        //新增图库
        Pics pic = new Pics();
        pic.setPic(goods.getPic());
        int picCount = picsDao.selectCount(pic);
        // 图库里没有此图的才会新增
        if (picCount == 0) {
            Pics pics = new Pics();
            pics.setCategory("goodsGallery");// 菜品图库
            pics.setPic(goods.getPic());
            pics.setGoodsId(goods.getId());
            pics.setShopId(goods.getShopId());
            pics.setCateId(goods.getCategoryId());
            pics.setGoodsName(goods.getName());
            pics.setUploadTime(new Date());
            picsDao.insert(pics);
        }
        //新增规格
        List<GoodsSpecification> specList = goods.getSpecList();
        for (GoodsSpecification spec : specList) {
            spec.setGoodsId(goods.getId());
            goodsSpecificationDao.insert(spec);
        }
        return 1;
    }

    /**
     * 更新
     *
     * @param goods 菜品
     * @return 成功是1
     */
    @Override
    public int update(Goods goods) {
        if (StringUtils.isBlank(goods.getId())) {
            return 0;
        }
        int result = goodsDao.update(goods);
        if (result != 1) {
            return 0;
        }
        //新增图库
        Pics pic = new Pics();
        pic.setPic(goods.getPic());
        int picCount = picsDao.selectCount(pic);
        // 图库里没有此图的才会新增
        if (picCount == 0) {
            Pics pics = new Pics();
            pics.setCategory("goodsGallery");// 菜品图库
            pics.setPic(goods.getPic());
            pics.setGoodsId(goods.getId());
            pics.setShopId(goods.getShopId());
            pics.setCateId(goods.getCategoryId());
            pics.setGoodsName(goods.getName());
            pics.setUploadTime(new Date());
            picsDao.insert(pics);
        }
        //新增规格
        List<GoodsSpecification> specList = goods.getSpecList();
        if (specList != null && specList.size() > 0) {
            for (GoodsSpecification spec : specList) {
                //如果有id,就编辑
                if (StringUtils.isNotBlank(spec.getId())) {
                    goodsSpecificationDao.update(spec);
                }
                //如果没有id,就新增
                if (StringUtils.isBlank(spec.getId())) {
                    spec.setGoodsId(goods.getId());
                    goodsSpecificationDao.insert(spec);
                }
            }
        }
        //删除规格
        if (StringUtils.isNotBlank(goods.getDelSpecIds())) {
            String[] delSpecIds = goods.getDelSpecIds().split(",");
            if (delSpecIds.length > 0) {
                for (String id : delSpecIds) {
                    goodsSpecificationDao.delete(id);
                }
            }
        }
        return 1;
    }

    /**
     * 根据用户查询列表
     *
     * @param parameter 参数
     * @return 列表
     */
    @Override
    public PageInfo selectListByUser(Object parameter) {
        PageInfo pageInfo = super.selectListByUser(parameter);
        List<Goods> goodsList = pageInfo.getList();
        goodsList.forEach(item -> {
            Category category = categoryDao.selectByPrimaryKey(item.getCategoryId());
            if (category != null) {
                item.setCateName(category.getName());
            }
        });
        return pageInfo;
    }
}
